package com.treasure.mall.zbqmallapi.controller;

import com.treasure.mall.biz.entity.common.CityAreaPO;
import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.service.core.BaseConfigService;
import com.treasure.mall.biz.service.core.app.AppCheckUpdateService;
import com.treasure.mall.biz.service.core.marketing.UserSubscribeRedPacketService;
import com.treasure.mall.biz.service.core.user.WxSubscribeUserService;
import com.treasure.mall.zbq.common.annotation.CurrentUser;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbqmallapi.annotation.AccessControl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/7/8
 */
@RestController
@RequestMapping("/common")
@AccessControl
@Api(tags = "通用模块")
public class CommonController {
    @Autowired
    private BaseConfigService baseConfigService;

    @Autowired
    private AppCheckUpdateService appCheckUpdateService;
    @Autowired
    private WxSubscribeUserService wxSubscribeUserService;
    @Autowired
    private UserSubscribeRedPacketService subscribeRedPacketService;

    /**
     * 获取行政区域
     *
     * @param pid
     * @return
     */
    @GetMapping("/cities")
    public ApiResult cities(@RequestParam(required = false, defaultValue = "0") int pid) {
        List<CityAreaPO> cityAreaPOS = baseConfigService.getCities(pid);

        return ApiResult.ok(cityAreaPOS);
    }


    @ApiOperation("检查更新")
    @GetMapping("/checkUpdate")
    public ApiResult checkUpdate() {
        return ApiResult.ok(appCheckUpdateService.checkUpdate());
    }


    @GetMapping("/hideLogin")
    public ApiResult hideLogin() {
        return ApiResult.ok(true);
    }


    /**
     * 检查是关注过公众号
     *
     * @param userPO
     * @return
     */
    @GetMapping("/check/subscribe")
    public ApiResult checkSubscribe(@CurrentUser UserPO userPO) {
        Map<String, Object> map = new HashMap<>(3);

        boolean isSubscribe = true;
        if (userPO != null) {
            isSubscribe = wxSubscribeUserService.checkSubscribe(userPO.getUserId());
            if (!isSubscribe && subscribeRedPacketService.checkUsed(userPO.getUserId())) {
                isSubscribe = true;
            }
        }
        map.put("subscribe", isSubscribe);
        map.put("authWechat", userPO != null && userPO.getWechatUser().getOpenId() != null);
        double amount = 0;
        if (userPO != null) {
            amount = subscribeRedPacketService.getAmount(userPO.getUserId());
        }
        //红包金额
        map.put("redAmount", amount);
        return ApiResult.ok(map);
    }

}
